package cn.edu.sgu.www.mhxysy.function.computer.score.impl;

import cn.edu.sgu.www.mhxysy.entity.chongwu_shizhuang.ChongwuShizhuang;
import cn.edu.sgu.www.mhxysy.entity.chongwu_shizhuang.ChongwuShizhuangAttribute;
import cn.edu.sgu.www.mhxysy.entity.chongwu_shizhuang.ChongwuShizhuangCategory;
import cn.edu.sgu.www.mhxysy.entity.chongwu_shizhuang.ChongwuShizhuangViewAttribute;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.score.ScoreComputer;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_shizhuang.ChongwuShizhuangAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_shizhuang.ChongwuShizhuangCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_shizhuang.ChongwuShizhuangMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_shizhuang.ChongwuShizhuangViewAttributeMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 宠物时装评分计算器
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class ChongwuShizhuangScoreComputer implements ScoreComputer {

    private final ChongwuShizhuangMapper chongwuShizhuangMapper;
    private final ChongwuShizhuangCategoryMapper chongwuShizhuangCategoryMapper;
    private final ChongwuShizhuangAttributeMapper chongwuShizhuangAttributeMapper;
    private final ChongwuShizhuangViewAttributeMapper chongwuShizhuangViewAttributeMapper;

    @Autowired
    public ChongwuShizhuangScoreComputer(
            ChongwuShizhuangMapper chongwuShizhuangMapper,
            ChongwuShizhuangCategoryMapper chongwuShizhuangCategoryMapper,
            ChongwuShizhuangAttributeMapper chongwuShizhuangAttributeMapper,
            ChongwuShizhuangViewAttributeMapper chongwuShizhuangViewAttributeMapper) {
        this.chongwuShizhuangMapper = chongwuShizhuangMapper;
        this.chongwuShizhuangCategoryMapper = chongwuShizhuangCategoryMapper;
        this.chongwuShizhuangAttributeMapper = chongwuShizhuangAttributeMapper;
        this.chongwuShizhuangViewAttributeMapper = chongwuShizhuangViewAttributeMapper;
    }

    /**
     * 计算宠物时装评分
     * @param shizhuangId 宠物时装ID
     * @return Integer 星石的评分
     */
    @Override
    public Integer apply(String shizhuangId) {
        ChongwuShizhuang chongwuShizhuang = chongwuShizhuangMapper.selectById(shizhuangId);

        if (chongwuShizhuang == null) {
            throw new GlobalException(ResponseCode.CONFLICT, "操作失败，宠物时装不存在！");
        }

        QueryWrapper<ChongwuShizhuangAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("shizhuang_id", shizhuangId);

        ChongwuShizhuangAttribute attribute = chongwuShizhuangAttributeMapper.selectOne(queryWrapper);

        if (attribute != null) {
            // 查询宠物时装类型信息
            Integer categoryId = chongwuShizhuang.getCategoryId();
            ChongwuShizhuangCategory chongwuShizhuangCategory = chongwuShizhuangCategoryMapper.selectById(categoryId);

            // 查询对应等级的预览属性
            QueryWrapper<ChongwuShizhuangViewAttribute> querywrapper = new QueryWrapper<>();

            querywrapper.eq("grade", chongwuShizhuangCategory.getGrade());
            querywrapper.eq("property_id", attribute.getPropertyId());

            ChongwuShizhuangViewAttribute viewAttribute = chongwuShizhuangViewAttributeMapper.selectOne(querywrapper);

            if (viewAttribute != null) {
                Integer max = viewAttribute.getMax();
                Integer propertyValue = attribute.getPropertyValue();

                // 评分 = <向下取整>[(属性值 × 16) / 最大值]
                BigDecimal score = BigDecimal.valueOf(propertyValue).multiply(
                        BigDecimal.valueOf(16)
                ).divide(BigDecimal.valueOf(max), BigDecimal.ROUND_FLOOR);

                return score.intValue();
            }
        }

        return 0;
    }

}